﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Data;
using InstituteManager.Bussiness;
using System.Data;
using System.Security.Cryptography;

namespace InstituteManager.adm
{
    public partial class Competition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                gridBind();
                GeneralDateControls(ddlStartDateDay, ddlStartDateMonth, ddlStartDateYear);
                GeneralDateControls(ddlEndDateDay,ddlEndDateMonth,ddlEndDateYear);
            }
        }
        protected void gridBind()
        {
            grvView.DataSource = CompetitionBL.getByAll();
            grvView.DataBind();
            ShowStaff();
        }

        void ShowStaff()
        {
            ddlStaffId.DataSource = StaffBL.getByPermission(3);
            ddlStaffId.DataTextField = "StaffName";
            ddlStaffId.DataValueField = "StaffId";
            ddlStaffId.DataBind();

            ddlAwardId.DataSource = AwardBL.getByAll();
            ddlAwardId.DataTextField = "AwardName";
            ddlAwardId.DataValueField = "AwardId";
            ddlAwardId.DataBind();
        }
        protected void GeneralDateControls(DropDownList ddlDay, DropDownList ddlMon, DropDownList ddlYear)
        {
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1; i <= 12; i++)
                ddlMon.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1900; i <= DateTime.Now.Year+20; i++)
                ddlYear.Items.Add(new ListItem(i + "", i + ""));
        }

        protected void ResetControl()
        {
            txtCompetitionName.Text = "";
            txtCondition.Text = "";

            ddlStartDateDay.SelectedValue = DateTime.Now.Day.ToString();
            ddlStartDateMonth.SelectedValue = DateTime.Now.Month.ToString();
            ddlStartDateYear.SelectedValue = DateTime.Now.Year.ToString();

            ddlEndDateDay.SelectedValue = DateTime.Now.Day.ToString();
            ddlEndDateMonth.SelectedValue = DateTime.Now.Month.ToString();
            ddlEndDateYear.SelectedValue = DateTime.Now.Year.ToString();

            txtId.Text = "";

        }

        protected void lbtAddnew_Click(object sender, EventArgs e)
        {
            pnlView.Visible = false;
            pnlUpdate.Visible = true;
            ResetControl();
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            bool result = true;
            bool isChecked = false;
            foreach (GridViewRow row in grvView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)row.FindControl("chkSelect")).Checked == true)
                    {
                        isChecked = true;
                        if (!CompetitionBL.Delete(int.Parse(grvView.DataKeys[row.RowIndex].Value.ToString())))
                            result = false;
                    }
                }
            }

            if (isChecked == false)
                ShowMessage(-1, "You have not selected data to delete ! ");
            else if (result)
            {
                ShowMessage(1, "Data deleted successfully ! ");
                gridBind();
            }
            else
            {
                ShowMessage(0, "Data deleted fail : " + CompetitionBL.errorMsg);
            }
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            gridBind();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            string strId = txtId.Text;

            Data.tbl_Competition entt = new Data.tbl_Competition();
            entt.CompetitionName = txtCompetitionName.Text;
            if (ddlStartDateDay.SelectedIndex > 0 && ddlStartDateMonth.SelectedIndex > 0 && ddlStartDateYear.SelectedIndex > 0)
                entt.StartDate = new DateTime(int.Parse(ddlStartDateYear.SelectedValue), int.Parse(ddlStartDateMonth.SelectedValue), int.Parse(ddlStartDateDay.SelectedValue));
            else
            {
                ShowMessage(-1, "You must choice Start Date of Competition !");
                return;
            }
            if (ddlEndDateDay.SelectedIndex > 0 && ddlEndDateMonth.SelectedIndex > 0 && ddlEndDateYear.SelectedIndex > 0)
                entt.EndDate = new DateTime(int.Parse(ddlEndDateYear.SelectedValue), int.Parse(ddlEndDateMonth.SelectedValue), int.Parse(ddlEndDateDay.SelectedValue));
            else
            {
                ShowMessage(-1, "You must choice End Date of Competition !");
                return;
            }

            if (entt.StartDate > entt.EndDate)
            {
                ShowMessage(0, "Start Date and End Date is invalid ! Start Date > End Date ");
                return;
            }

            entt.Condition = txtCondition.Text;

            
            
            // if strId = "" then add new else update
            if (strId.Trim() == "")
            {
                if (CompetitionBL.AddNew(entt, Convert.ToInt32(ddlStaffId.SelectedValue), Convert.ToInt32(ddlAwardId.SelectedValue)))
                {
                    ShowMessage(1, "Data added successfully ! ");
                    gridBind();
                }
                else
                {
                    ShowMessage(0, "Data added fail : " + CompetitionBL.errorMsg);
                }
            }
            else
            {
                entt.CompetitionId = int.Parse(strId);
                if (CompetitionBL.Update(entt, Convert.ToInt32(ddlStaffId.SelectedValue), Convert.ToInt32(ddlAwardId.SelectedValue)))
                {
                    ShowMessage(1, "Data updated successfully ! ");
                    gridBind();
                }
                else
                {
                    ShowMessage(0, "Data updated fail : " + CompetitionBL.errorMsg);
                }
            }

            pnlView.Visible = true;
            pnlUpdate.Visible = false;
            gridBind();
        }

        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
            pnlUpdate.Visible = false;
        }

        protected void grvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvView.PageIndex = e.NewPageIndex;
            gridBind();
        }

        protected void grvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "cmdEdit":
                    txtId.Text = strId;
                    tbl_Competition obj = CompetitionBL.getById(int.Parse(strId));                    
                    txtCompetitionName.Text = obj.CompetitionName;
                    if (obj.StartDate != null)
                    {
                        ddlStartDateDay.SelectedValue = obj.StartDate.Value.Day.ToString();
                        ddlStartDateMonth.SelectedValue = obj.StartDate.Value.Month.ToString();
                        ddlStartDateYear.SelectedValue = obj.StartDate.Value.Year.ToString();
                    }
                    if (obj.EndDate != null)
                    {
                        ddlEndDateDay.SelectedValue = obj.EndDate.Value.Day.ToString();
                        ddlEndDateMonth.SelectedValue = obj.EndDate.Value.Month.ToString();
                        ddlEndDateYear.SelectedValue = obj.EndDate.Value.Year.ToString();
                    }
                    txtCondition.Text = obj.Condition;
                    if (obj.tbl_Staff != null)
                        ddlStaffId.SelectedValue = obj.tbl_Staff.StaffId.ToString();
                    if (obj.tbl_Award != null)
                        ddlAwardId.SelectedValue = obj.tbl_Award.AwardId.ToString();

                    pnlUpdate.Visible = true;
                    pnlView.Visible = false;
                    break;
                case "cmdView":
                    obj = CompetitionBL.getById(int.Parse(strId));
                    lblCompetitionName.Text = obj.CompetitionName;
                    if (obj.StartDate != null)
                        lblStartDate.Text = obj.StartDate.Value.ToString("d");
                    if (obj.EndDate != null)
                        lblEndDate.Text = obj.EndDate.Value.ToString("d");

                    lblStaffName.Text = obj.tbl_Staff.StaffName;
                   
                    lblAward.Text = obj.tbl_Award.AwardName;

                    pnlView.Visible = false;
                    pnlDetail.Visible = true;
                    break;
                case "cmdDelete":
                    if (CompetitionBL.Delete(int.Parse(strId)))
                    {
                        ShowMessage(1, "Data deleted successfully ! ");
                        gridBind();
                    }
                    else
                    {
                        ShowMessage(0, "Data deleted fail : " + CompetitionBL.errorMsg);
                    }
                    break;
            }
        }

        protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onClick", "SelectAllClick(this)");
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onClick", "SelectClick(this)");
        }
        
        protected void lbtBackDetail_Click(object sender, EventArgs e)
        {
            pnlDetail.Visible = false;
            pnlView.Visible = true;
        }

        protected void ibtSearch_Click(object sender, ImageClickEventArgs e)
        {
            grvView.DataSource = CompetitionBL.SearchByName(txtSearch.Text);
            DataBind();
        }
    }
}